package com.cuitrip.business.invite;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.apiservice.UserBusiness;
import com.cuitrip.business.bill.AccountAddEvent;
import com.cuitrip.business.invite.model.InviteDetail;
import com.cuitrip.business.invite.model.InviteList;
import com.cuitrip.component.item.CtTextView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.text.TextProxy;
import com.lab.adapter.IAdapterData;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.component.list.CTRecyclerView;
import com.lab.component.list.simple.CTSimpleRecyclerAdapter;
import com.lab.component.list.simple.CTSimpleViewHolder;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.rx.bus.RxBus;
import com.lab.utils.DisplayUtil;
import com.lab.utils.MessageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by sheena on 16/5/19.
 */
public class InviteDetailFragment extends CustomUiFragment implements IProxyCallback{

    @Bind(R.id.rv_invite_list)
    CTRecyclerView mCtRecyclerView;

    @Bind(R.id.tv_invite_num)
    CtTextView mInviteTitleLayout;

    @Bind(R.id.tv_reward_rule)
    CtTextView mRewardRule;

    private ArrayList<InviteDetail> inviteFriendsList = new ArrayList<>();
    private CTSimpleRecyclerAdapter mAdapter;
    private ApiProxy mApiProxy=new ApiProxy(this);
    private int num;
    private int money;

    public static InviteDetailFragment newInstance( int inviteNum,int money){
        InviteDetailFragment inviteDetailFragment=new InviteDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("num",inviteNum);
        bundle.putInt("money",money);
        inviteDetailFragment.setArguments(bundle);
        return inviteDetailFragment;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mLeftStyle = CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.user_invite_detail_title);
        return config;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_invite_friend_detail);
        if (getArguments()!=null){
            num = getArguments().getInt("num", 0);
            money = getArguments().getInt("money", 0);
        }
        return view;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this, getView());
    }

    @Override
    public void initData() {
        showLoading();
        UserBusiness.getInviteList(mApiProxy);
        TextProxy.getInstance().formatWithSingleAndMore(num,R.string.user_invite_number_2,R.string.user_invite_number_1);
        mInviteTitleLayout.setText(getString(R.string.user_invite_number_1,num));
        mRewardRule.setText(getString(R.string.user_invite_rule,money));
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(AppCoreRuntime.context, LinearLayoutManager.VERTICAL, false);
        mCtRecyclerView.setLayoutManager(mLayoutManager);
        mCtRecyclerView.hideProgress();
        mAdapter = new CTSimpleRecyclerAdapter<InviteDetail>(AppCoreRuntime.context, inviteFriendsList) {

            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_invite_detail;
            }

            @Override
            public void onNewViewHolder(CTSimpleViewHolder viewHolder, ViewGroup parent, int viewType) {

            }

            @Override
            public void bindData(CTSimpleViewHolder holder, int position, InviteDetail item) {
                TextView name=holder.getView(R.id.tv_name);
                TextView state=holder.getView(R.id.tv_state);
                TextView reward=holder.getView(R.id.tv_reward);

                name.setText(item.getNick());
                state.setText(item.getEventName());
                reward.setText("$ "+item.getMoney());
            }
        };
        mCtRecyclerView.setAdapter(mAdapter);

    }

    @Override
    public void onTopbarLeftPress() {
        onBackPressed();
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        if (!isAdded()){
            return false;
        }
        CtApiResponse response= (CtApiResponse) result.getData();
        if (response.isResponseNormal()){
            InviteList inviteList= (InviteList) response.result;
            inviteFriendsList.clear();
            inviteFriendsList.addAll(inviteList.getList());
            mAdapter.notifyDataSetChanged();
            RxBus.getDefault().send(new InviteEvent());
        }else {
            if(!TextUtils.isEmpty(response.msg)){
                MessageUtils.showToast(getHostActivity(),response.msg);
            }
        }
        hideLoading();
        return false;
    }

}
